package com.lyn.module_custom_dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import androidx.annotation.NonNull;
import com.lyn.module_custom_base.dialog.BaseDialog;
import com.lyn.module_custom_dialog.databinding.DialogPasswordBinding;

/**
 * 密码输入框
 */
public class PasswordDialog extends BaseDialog<DialogPasswordBinding> {


    private boolean customLengthAndWidth = false;
    private boolean dispatchKeyEvent = false;
    private String title;
    private int passwordLength = 6;
    private InputPasswordCallback callback;

    @Override
    public void setCustomLengthAndWidth(boolean customLengthAndWidth) {
        this.customLengthAndWidth = customLengthAndWidth;
    }

    @Override
    public void setDispatchKeyEvent(boolean dispatchKeyEvent) {
        this.dispatchKeyEvent = dispatchKeyEvent;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setCallback(InputPasswordCallback callback) {
        this.callback = callback;
    }

    public PasswordDialog(@NonNull Context context) {
        super(context, R.style.common_dialog);
    }

    public PasswordDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected DialogPasswordBinding getViewBinding() {
        return DialogPasswordBinding.inflate(getLayoutInflater(), null, false);
    }

    @Override
    protected void bindData(DialogPasswordBinding binding, Dialog dialog) {
        if (!TextUtils.isEmpty(title)) {
            binding.title.setText(title);
        }
        InputFilter[] filters = {new InputFilter.LengthFilter(passwordLength)};
        binding.password.setFilters(filters);
    }

    @Override
    protected void bindListener(DialogPasswordBinding binding, Dialog dialog) {
        binding.password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String string = s.toString();
                if (!TextUtils.isEmpty(string)) {
                    if (string.length() >= passwordLength) {
                        if (callback != null) {
                            callback.onCallback(string,PasswordDialog.this);
                        }
                    }
                }
            }
        });
    }

    @Override
    protected boolean getDispatchKeyEvent() {
        return dispatchKeyEvent;
    }

    @Override
    protected boolean customLengthAndWidth() {
        return customLengthAndWidth;
    }

    public void clearPassword() {
        if (binding != null) {
            binding.password.setText("");
        }
    }

    public interface InputPasswordCallback {
        void onCallback(String password,Dialog dialog);
    }
}
